﻿using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace HareGame
{
    [CustomEditor(typeof(LocalizationText))]
    public class LocalizationTextEditor : UnityEditor.UI.TextEditor
    {
        bool isInit = false;
        List<LocalizationData> m_Language = new List<LocalizationData>();
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            LocalizationText component = (LocalizationText)target;
            component.Key = EditorGUILayout.TextField("Key", component.Key);
            component.Language = (SystemLanguage)EditorGUILayout.EnumPopup("Language(仅供测试排版使用)", component.Language);
            component.text = GetValue(component.Key, component.Language);
            if (GUI.changed)
            {
                EditorUtility.SetDirty(target);
            }
        }

        public void Init()
        {
            m_Language = Util.Instance.ReadJsonDataFromStreamAssets<LocalizationData>("Localization");
            isInit = true;
        }

        public string GetValue(string key, SystemLanguage language)
        {
            if (!isInit)
            {
                Init();
            }
            string value = "请输入正确的Key";
            foreach (var v in m_Language)
            {
                if (v.Key == key)
                {
                    switch (language)
                    {
                        case SystemLanguage.English:
                            value = v.English;
                            break;
                        case SystemLanguage.Chinese:
                        case SystemLanguage.ChineseSimplified:
                            value = v.Chinese;
                            break;
                        case SystemLanguage.ChineseTraditional:
                            value = v.ChineseTraditional;
                            break;
                        case SystemLanguage.German:
                            value = v.German;
                            break;
                        case SystemLanguage.Spanish:
                            value = v.Spanish;
                            break;
                        case SystemLanguage.French:
                            value = v.French;
                            break;
                        case SystemLanguage.Indonesian:
                            value = v.Indonesian;
                            break;
                        case SystemLanguage.Italian:
                            value = v.Italian;
                            break;
                        case SystemLanguage.Japanese:
                            value = v.Japanese;
                            break;
                        case SystemLanguage.Korean:
                            value = v.Korean;
                            break;
                        case SystemLanguage.Portuguese:
                            value = v.Portuguese;
                            break;
                        case SystemLanguage.Russian:
                            value = v.Russian;
                            break;
                        case SystemLanguage.Thai:
                            value = v.Thai;
                            break;
                        case SystemLanguage.Turkish:
                            value = v.Turkish;
                            break;
                        case SystemLanguage.Vietnamese:
                            value = v.Vietnamese;
                            break;
                        default:
                            value = v.English;
                            break;
                    }
                }
            }
            return value;
        }
    }
}